import { setToken, getToken } from '@/utils/auth'
import { sysLogin, sysProfile, sysUser } from '@/api/user'
const state = {
  token: getToken() || '',
  userInfo: ''
}
const getters = {}
const mutations = {
  mutToken(state, value) {
    state.token = value
    console.log('getToken()', getToken())
    setToken(value)
  },
  mutUserInfo(state, value) {
    state.userInfo = value
  },
  mutLogout(state) {
    state.token = ''
    setToken('')
    state.userInfo = ''
  }
}
const actions = {
  async actToken({ commit }, value) {
    const res = await sysLogin(value)
    console.log('res', res)
    commit('mutToken', res.data)
  },
  async actUserInfo({ commit }) {
    const res = await sysProfile()
    const res2 = await sysUser(res.data.userId)
    // console.log('获取个人用户信息', res)
    commit('mutUserInfo', { ...res.data, ...res2.data })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
